package com.cw.jeeyt.dao.impl.mdata;

import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

import com.cw.jeeyt.dao.intf.mdata.MdataBanksDao;
import com.cw.jeeyt.dao.mapper.mdata.MdataBanksMapper;
import com.cw.jeeyt.dao.model.mdata.MdataBanksDo;
import com.cw.jeeyt.dao.model.mdata.MdataBanksExample;
import com.cw.jeeyt.dao.model.mdata.MdataBanksKeyDo;
import com.cw.jeeyt.dao.query.mdata.MdataBanksQuery;
import com.cw.lang.common.dao.AbstractBaseDaoImpl;
import com.cw.lang.common.pagehelper.PageHelper;
import com.cw.lang.common.utils.StringUtil;
import com.cw.lang.mybatis.dto.PageInfo;
import com.cw.lang.mybatis.dto.PageParam;

/**
 * 银行数据库操作接口实现
 *
 * @author admin
 * @date 2021-03-26 22:30:39
 */
@Repository
public class MdataBanksDaoImpl extends AbstractBaseDaoImpl<MdataBanksDo, MdataBanksMapper> implements MdataBanksDao {
    /**
     * 分页查询
     *
     * @param mdataBanks 条件
     * @param pageParam 分页参数
     * @return
     */
    @Override
    public PageInfo<MdataBanksDo> selectPage(MdataBanksQuery mdataBanks, PageParam pageParam) {
        MdataBanksExample example = buildExample(mdataBanks);
        return PageHelper.<MdataBanksDo>startPage(pageParam.getPageNum(), pageParam.getPageSize()).doSelectPageInfo(
            () -> getMapper().selectByExample(example));
    }

    /**
     * 根据key查询银行
     *
     * @param bankId
     * @return
     */
    @Override
    public MdataBanksDo selectByKey(String bankId) {
        MdataBanksKeyDo mdataBanksKeyDo = new MdataBanksKeyDo();
        mdataBanksKeyDo.setBankId(bankId);
        return getMapper().selectByPrimaryKey(mdataBanksKeyDo);
    }

    /**
     * 根据key删除银行
     *
     * @param bankId
     * @return
     */
    @Override
    public int deleteByKey(String bankId) {
        MdataBanksKeyDo mdataBanksKeyDo = new MdataBanksKeyDo();
        mdataBanksKeyDo.setBankId(bankId);
        return getMapper().deleteByPrimaryKey(mdataBanksKeyDo);
    }

    /**
     * 查询银行信息
     *
     * @param mdataBanks 条件
     * @return List<MdataBanksDo>
     */
    @Override
    public List<MdataBanksDo> selectByExample(MdataBanksQuery mdataBanks) {
        return getMapper().selectByExample(buildExample(mdataBanks));
    }

    /**
     * 新增银行信息
     *
     * @param mdataBanks 条件
     * @return int>
     */
    @Override
    public int insertBySelective(MdataBanksDo mdataBanks) {
        if (mdataBanks == null) {
            return -1;
        }
        mdataBanks.setCreateTime(new Date());
        mdataBanks.setUpdateTime(new Date());
        return getMapper().insertSelective(mdataBanks);
    }

    /**
     * 修改银行信息
     *
     * @param mdataBanks
     * @return
     */
    @Override
    public int updateBySelective(MdataBanksDo mdataBanks) {
        if (mdataBanks == null) {
            return -1;
        }
        mdataBanks.setUpdateTime(new Date());
        return getMapper().updateByPrimaryKeySelective(mdataBanks);
    }

    /**
     * 构建银行Example信息
     *
     * @param mdataBanks
     * @return
     */
    public MdataBanksExample buildExample(MdataBanksQuery mdataBanks) {
        MdataBanksExample example = new MdataBanksExample();
        MdataBanksExample.Criteria criteria = example.createCriteria();
        if (mdataBanks != null) {
            //添加查询条件
            if (StringUtil.isNotEmpty(mdataBanks.getBankId())) {
                criteria.andBankIdEqualTo(mdataBanks.getBankId());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getBankName())) {
                criteria.andBankNameEqualTo(mdataBanks.getBankName());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getBankNo())) {
                criteria.andBankNoEqualTo(mdataBanks.getBankNo());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getBigLogo())) {
                criteria.andBigLogoEqualTo(mdataBanks.getBigLogo());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getClsbank())) {
                criteria.andClsbankEqualTo(mdataBanks.getClsbank());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getDtype())) {
                criteria.andDtypeEqualTo(mdataBanks.getDtype());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getRemark())) {
                criteria.andRemarkEqualTo(mdataBanks.getRemark());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getSmallLogo())) {
                criteria.andSmallLogoEqualTo(mdataBanks.getSmallLogo());
            }
            if (StringUtil.isNotEmpty(mdataBanks.getStatus())) {
                criteria.andStatusEqualTo(mdataBanks.getStatus());
            }
        }
        //添加排序字段
        example.setOrderByClause(" create_date desc");
        return example;
    }
}
